<template>
	<div class="system-dic-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-user-search mb15">
				<el-input size="default" placeholder="请输入公司名称" style="max-width: 180px" v-model="state.tableData.param.param.title.val"> </el-input>
				<el-button size="default" type="primary"  @click="seachClick">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="success" v-auth="'/group/company/add'"  @click="onOpenAddCompany('add')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增公司
				</el-button>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column prop="id" label="ID" show-overflow-tooltip width="80px"></el-table-column>
				<el-table-column prop="title" label="公司名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="user_num" label="员工上限" show-overflow-tooltip>
					<template #default="scope">
						<span>{{`${scope.row.user_total}/${scope.row.user_num || '无上限'}`}}</span>
					</template>
				</el-table-column>
				<el-table-column prop="status" label="公司状态" show-overflow-tooltip>
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status == 1">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="创建时间" show-overflow-tooltip>
					<template #default="scope">
						<span>{{ dateFormatYMDHMS(scope.row.create_time * 1000)}}</span>
                    </template>
				</el-table-column>
				<el-table-column label="操作" width="180">
					<template #default="scope">
						<el-button size="small" text type="primary" v-auth="'/group/platform/info'"  @click="onOpenPaltform(scope.row)">渠道</el-button>
						<el-button size="small" text type="primary" v-auths="['/group/company_conf/index','/group/company_conf/conf']"  @click="onOpenPaltformConfig(scope.row)">配置</el-button>
						<el-button size="small" text type="primary" v-auth="'/group/company/edit'"  @click="onOpenEditCompany('edit', scope.row)">修改</el-button>
						<el-button size="small" text type="primary" v-auth="'/group/company/del'"  @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				:current-page="state.tableData.param.page"
				background
				:page-size="state.tableData.param.limit"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total"
			>
			</el-pagination>
		</el-card>
		<el-dialog :title="state.paltformData.title" v-model="state.paltformData.isShowDialog" width="450px"
			:close-on-click-modal="false">
			<div style="min-height: 200px;" v-if="state.paltformData.paltforms.length > 0">
				<div style="display: flex;flex-wrap: wrap;gap: 5px;">
					<el-tag v-for="(item,index) in state.paltformData.paltforms" :key="index">{{item.cname}}</el-tag>
				</div>
				
			</div>
			<div style="text-align: center;margin-top: 10px;height: 200px;line-height: 200px;" v-else-if="!state.paltformData.loading">暂无渠道</div>
			<div style="width: 100%;height: 200px;" v-else v-loading="state.paltformData.loading"></div>
		</el-dialog>
		<companyDialog ref="companyDialogRef" @refresh="getTableData()" />
		<companyConfigDialog ref="companyConfigDialogRef"/>
	</div>
</template>

<script setup name="company">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { companyApi } from '/@/api/company/index';
import { platformApi } from '/@/api/platform/index';
import commonFunction from '/@/utils/commonFunction';
// 定义变量内容
const { dateFormatYMDHMS } = commonFunction();
// 引入组件
const companyDialog = defineAsyncComponent(() => import('./dialog.vue'));
// 引入组件
const companyConfigDialog = defineAsyncComponent(() => import('./configDialog.vue'));
const paramOp = {
	title:'%%'
}
// 定义变量内容
const companyDialogRef = ref();
const companyConfigDialogRef = ref();
const state = reactive({
	paltformData:{
		title:'',
		isShowDialog:false,
		paltforms:[],
		loading:false
	},
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			param:{
				title:{},
			},
			page: 1,
			limit: 10,
		},
	},
});
//配置
const onOpenPaltformConfig = (row) => {
	companyConfigDialogRef.value.openDialog(row);

}
const onOpenPaltform = (row) => {
	state.paltformData.title = row.title;
	state.paltformData.isShowDialog = true;
	state.paltformData.loading = true;
	state.paltformData.paltforms = []
	platformApi().info({id:row.platform}).then(res => {
		if(res.data){
			state.paltformData.paltforms = res.data;
		}
		state.paltformData.loading = false;
	})
};
// 初始化表格数据
const getTableData = () => {
	state.tableData.loading = true;
	for (let key in state.tableData.param.param) {
		if (state.tableData.param.param[key].val && state.tableData.param.param[key].val !== '') {
			state.tableData.param.param[key].op = paramOp[key] || '='
		}else{
			state.tableData.param.param[key] = {}
		}
	}
    companyApi().index(state.tableData.param).then(res=>{
        state.tableData.data = res.data.list;
        state.tableData.total = res.data.total;
        state.tableData.loading = false;
    })
};
// 打开新增公司弹窗
const onOpenAddCompany = (type) => {
	companyDialogRef.value.openDialog(type);
};
// 打开修改公司弹窗
const onOpenEditCompany = (type, row) => {
	companyDialogRef.value.openDialog(type, row);
};
// 删除公司
const onRowDel = (row) => {
	ElMessageBox.confirm(`此操作将永久删除公司名称：“${row.title}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			companyApi().del({ids:row.id}).then(res=>{
                getTableData();
                ElMessage.success('删除成功');
            })
		})
		.catch(() => {});
};
const seachClick = () =>{
	onHandleCurrentChange(1)
}
// 分页改变
const onHandleSizeChange = (val) => {
	state.tableData.param.limit = val;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (val) => {
	state.tableData.param.page = val;
	getTableData();
};
// 页面加载时
onMounted(() => {
	getTableData();
});
</script>
